#include <iostream>
#include <cmath>
#include <string>
#include "Main.hpp"
#include "Keith.hpp"
#include "Thread.hpp"

using std::cout;
using std::endl;

//==============================================================================================================
// Static Function Declarations
//==============================================================================================================
static bool IsKeith
    (
    ulong const pNum
    );
	
//==============================================================================================================
// Function Definitions
//==============================================================================================================
void FindKeiths(ulong pLimit){
	
	if(GetVerbose())
		cout << "FindKeiths() Begin" << endl;
	for(ulong n = 10L; n <= pLimit; n++){
		if(IsKeith(n))
			;
	}
		if(GetVerbose())
			cout << "FindKeiths() End" << endl;
}

void *FindKeithsThread(void *pState)
{
	if (GetVerbose()) cout << "FindKeithsThread() Begin" << endl;
	ThreadState *state = static_cast<ThreadState *>(pState);
	FindKeiths(state->mLimit);
	state->mExitCode = 0;
	if (GetVerbose()) cout << "FindKeithsThread() End" << endl;
	pthread_exit(pState);
}

static bool IsKeith(ulong const pNum){
	if (pNum < 10)
		return false;
	ulong num = pNum;
	int length = 1;
	ulong arr[128]; //using array for initial conditions

    while (num > 9) {
        num /= 10;
        length++;
    }

	//array of unknown size will be filled with initial conditions for sequence 
	int total = 0;
	num = pNum;
	for(int n = length-1; n >= 0; n--){
		arr[n] = num % 10;
		total += arr[n];
		num = num/10;
	} //arr[1,9,7] - if the number was 197, the array at this point would be this

	arr[length] = total;
	length++;

	while (arr[length-1] < pNum) {
		arr[length] = arr[length-1] + arr[length-2];
		if (arr[length] == pNum){
			return true;
		}
		length++;
	}

	return false;
}

